package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.model.hosp.Department;
import com.example.repository.DepartmentRepository;
import com.example.service.DepartmentService;
import com.example.vo.hosp.DepartmentQueryVo;
import com.example.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void save(Map<String, Object> parameterMap) {
        //parameterMap转换为department
        String parameterMapString = JSONObject.toJSONString(parameterMap);
        Department department = JSONObject.parseObject(parameterMapString,Department.class);

        //根据医院编号和科室编号查询
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 科室分页查询
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象，设置当前页、每页记录数
        //0是第一页
        Pageable pageable= PageRequest.of(page-1,limit);
        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                //模糊查询
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //忽略大小写
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除医院
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            //调用删除方法
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编号查询所有科室
    @Override
    public List<DepartmentVo> findDeptTree(String hosCode) {
        //创建list集合用于最终数据封装
        List<DepartmentVo> result=new ArrayList<>();
        //根据医院编号查询医院所有科室信息
        Department departmentQuery=new Department();
        departmentQuery.setHoscode(hosCode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表信息 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号 bigcode 分组，获得大科室每个下级子科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //大科室编号
            String bigCode = entry.getKey();
            //大科室code对应的全部的数据
            List<Department> departments = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departments.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children=new ArrayList<>();
            for (Department department : departments) {
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo1);
            }
            //小科室的list集合放到大科室的children里面
            departmentVo.setChildren(children);
            //放到最终的result里面
            result.add(departmentVo);
        }
        return result;
    }

    /**
     * 根据科室编号、医院编号查询科室名称
     * @param hoscode 医院编号
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
